#include "StdAfx.h"
#include "speed_calculator.h"

speed_calculator::speed_calculator(void)
{
	sample_count_ = 6;
}

speed_calculator::~speed_calculator(void)
{
}

void 
speed_calculator::set_sample(int trans_size)
{
	this->speed_list_.push_back(std::make_pair(trans_size, GetTickCount()));
	if ( this->speed_list_.size() > sample_count_ )
	{
		this->speed_list_.pop_front();
	}
}

int 
speed_calculator::get_speed()
{	
	speed_list::iterator pos;
	std::pair<int, DWORD> elem = this->speed_list_.front();
	DWORD time1 = elem.second;
	elem = this->speed_list_.back();

	DWORD interval = (elem.second - time1)/** this->sample_count_ / this->speed_list_.size()*/;

	if ( (interval == 0) | (this->sample_count_ > this->speed_list_.size() ) )
	{
		if ( interval < 500 )
		{
			interval = 500;
		}
		interval = interval * this->sample_count_;
	}

	DWORD trans_size = 0;
	pos = this->speed_list_.begin();
	for ( ; pos != this->speed_list_.end(); ++pos )
	{
		trans_size += pos->first;
	}
	//TRACE(_T("trans size: %d, interval: %d"), trans_size, interval);
	
	
	int speed = trans_size * 1000 / interval; 
	if ( speed > this->file_size_ )
	{
		speed = this->file_size_;
	}

	return speed;
}

void 
speed_calculator::set_file_size(int file_size)
{
	this->file_size_ = file_size;
	this->speed_list_.clear();
}

void
speed_calculator::set_sample_count(int sample_count)
{
	this->sample_count_ = sample_count;
}